package com.example.demo.controller;

import com.example.demo.Task.GetClassRoomEmptyTimeTask;
import com.example.demo.Task.GetScheduleTask;
import com.example.demo.Task.GetTeacherCourseTask;
import com.example.demo.exception.GetDataFailException;
import com.example.demo.exception.PasswordErrorException;
import com.example.demo.result.ApiResult;
import com.example.demo.service.EmptyClassRoomService;
import com.example.demo.service.ManagerService;
import com.example.demo.service.ScheduleService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.List;
import java.util.Timer;

/**
 * 获取数据
 *
 * @author lizijian
 */
@RestController
@RequestMapping("/School/Manager")
@Slf4j
public class ManagerController {

  @Autowired
  private ManagerService managerService;

  @Autowired
  private GetScheduleTask getScheduleTask;

  @Autowired
  private GetClassRoomEmptyTimeTask getClassRoomEmptyTimeTask;

  @Autowired
  private GetTeacherCourseTask getTeacherCourseTask;


  private static final ScheduledThreadPoolExecutor GET_SCHEDULE_POOL_EXECUTOR = new ScheduledThreadPoolExecutor(
      1, r -> new Thread(r, "getScheduleData"));

  private static final ScheduledThreadPoolExecutor GET_EMPTY_CLASSROOM_POOL_EXECUTOR = new ScheduledThreadPoolExecutor(
      1, r -> new Thread(r, "getEmptyClassRoom"));

  private static final ScheduledThreadPoolExecutor GET_TEACHER_COURSE_POOL_EXECUTOR = new ScheduledThreadPoolExecutor(
      1, r -> new Thread(r, "getTeacherCourse"));


  @Autowired
  private ScheduleService scheduleService;


  @ApiOperation(value = "模糊查询班级名")
  @ApiImplicitParam(name = "className", value = "班级名", required = true, paramType = "query")
  @RequestMapping("/queryClassName")
  public ApiResult<List<String>> queryClassName(@NotNull(message = "className不能为null") String className) {
    List<String> classNameList = scheduleService.fuzzyQueryClassName(className);
    return ApiResult.success(classNameList);
  }

  @ApiOperation(value = "爬取所有班级的课表信息")
  @RequestMapping("/getScheduleData")
  public ApiResult<?> getScheduleData() {
    GET_SCHEDULE_POOL_EXECUTOR.remove(getScheduleTask);
    GET_SCHEDULE_POOL_EXECUTOR.scheduleAtFixedRate(getScheduleTask, 0, 24, TimeUnit.HOURS);
    return ApiResult.success();
  }

  @ApiOperation(value = "爬取指定班级的课表")
  @ApiImplicitParam(name = "className", value = "班级名", required = true, paramType = "query")
  @RequestMapping("/getScheduleDataByClassName")
  public ApiResult<?> getScheduleByClassName(
      @NotNull(message = "className不能为null") String className)
      throws PasswordErrorException, IOException {
    scheduleService.insertSchedule(className);
    return ApiResult.success();
  }



  @ApiOperation(value = "获得空教室信息")
  @RequestMapping("/getEmptyClassRoom")
  public ApiResult<?> getEmptyClassRoom() {
    GET_EMPTY_CLASSROOM_POOL_EXECUTOR.remove(getClassRoomEmptyTimeTask);
    GET_EMPTY_CLASSROOM_POOL_EXECUTOR.scheduleAtFixedRate(getClassRoomEmptyTimeTask,0,24,TimeUnit.HOURS);
    return ApiResult.success();
  }

  @ApiOperation(value = "获取老师课程信息")
  @RequestMapping("/getTeacherCourse")
  public ApiResult<?> getTeacherCourse() {
    GET_TEACHER_COURSE_POOL_EXECUTOR.remove(getTeacherCourseTask);
    GET_TEACHER_COURSE_POOL_EXECUTOR.scheduleAtFixedRate(getTeacherCourseTask,0,24,TimeUnit.HOURS);
    return ApiResult.success();
  }

  @ApiOperation(value = "插入全校教室数据",notes = "不常用")
  @RequestMapping("/putCourseClassRoom")
  public ApiResult<?> putCourseClassRoom() throws PasswordErrorException, IOException {
    managerService.putCourseClassRoom();
    return ApiResult.success();
  }

  @ApiOperation(value = "插入湖工大选修课程时间表",notes = "每个学期导入一次")
  @RequestMapping("/putOptionalCourse")
  public ApiResult<?> putOptionalCourse() throws IOException {
    managerService.putOptionalCourse();
    return ApiResult.success();
  }

}
